using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SimulatorGui
{
    
    public partial class TapeControl : UserControl
    {
        private string _text;
        private int _currentIndex;
        private List<TextBox> _charsList;
        

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DefaultValue(0)]
        
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set { _currentIndex = value; this.Text = this.Text; 
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DefaultValue("")]
        public override string Text
        {
            
            get
            {
                return _text;
                //return base.Text;

            }
            set
            {
                int i;
                int t;
                /*
                StringBuilder _text = new StringBuilder(value);
                
                for (int a = 0; a < UInt16.MaxValue; a++)
                {
                    _text.Append(" ");
                }
                base.Text = _text.ToString();
                */
                _text = value;
                string charToPut;

                foreach (TextBox txt in _charsList)
                {
                    charToPut = " ";
                    t = Int32.Parse(txt.Name.Remove(0, 1));         //t=11
                    i = t - 10 + _currentIndex; //i = 1
                    if (i>=0)
                    {
                        if (i<this.Text.Length)
                        {
                            charToPut = this.Text[i].ToString();
                        }
                        
                    }
                    else
                    {
                        
                    }
                    txt.Text = charToPut;
                }
            }
        }
        public TapeControl()
        {
            InitializeComponent();
            InitCharList();
            ClearCharList();
            //_text = string.Empty;
            //_currentIndex = 0;
            this.Text = "#";
            this.CurrentIndex = 0;
            //this.CurrentIndex = 13;
            //this.Text = "#01234567890123456";
            this.Refresh();
        }

        #region Private methods
        private void InitCharList()
        {
            _charsList = new List<TextBox>();
            _charsList.Add(t0);
            _charsList.Add(t1);
            _charsList.Add(t2);
            _charsList.Add(t3);
            _charsList.Add(t4);
            _charsList.Add(t5);
            _charsList.Add(t6);
            _charsList.Add(t7);
            _charsList.Add(t8);
            _charsList.Add(t9);
            _charsList.Add(t10);
            _charsList.Add(t11);
            _charsList.Add(t12);
            _charsList.Add(t13);
            _charsList.Add(t14);
            _charsList.Add(t15);
            _charsList.Add(t16);
            _charsList.Add(t17);
            _charsList.Add(t18);
            _charsList.Add(t19);
            _charsList.Add(t20);
            
        }
        private void ClearCharList()
        {
            foreach (TextBox tb in _charsList)
            {
                tb.Text = string.Empty;
            }
        }
        #endregion

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
            
        }

        private void TapeControl_MouseEnter(object sender, EventArgs e)
        {
            toolTip1.Show(this.Text, this);
        }

        private void TapeControl_MouseLeave(object sender, EventArgs e)
        {
            toolTip1.Hide(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (Exception e1)
            {

                MessageBox.Show("Error: " + e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (Exception e1)
            {

                MessageBox.Show("Error: " + e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
